/*
 * PassHashRGUI.java
 *
 * Created on August 23, 2007, 4:50 PM
 */

package passhashr;

/**
 *
 * @author  Rahul Behera
 */

import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.UIManager;
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.File;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.awt.Toolkit;



public class PassHashRGUI extends javax.swing.JFrame {
    private boolean ranonce = false;
    private String[] letters = new String[66];
    private File file;
    private inout io = new inout();
    private PassHashR pshr = new PassHashR();
    private RandomGen rg = new RandomGen();
    /** Creates new form PassHashRGUI */
    public PassHashRGUI() {
        initComponents();
        setLocationRelativeTo(null);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        Encrypt = new javax.swing.JButton();
        Decrypt = new javax.swing.JButton();
        Copy = new javax.swing.JButton();
        Paste = new javax.swing.JButton();
        Save = new javax.swing.JButton();
        SecurityPower = new javax.swing.JTextField();
        OpenSecurityCode = new javax.swing.JButton();
        GenerateSecurityCode = new javax.swing.JButton();
        TopScrollPane = new javax.swing.JScrollPane();
        TopTextArea = new javax.swing.JTextArea();
        BottomScrollPane = new javax.swing.JScrollPane();
        BottomTextArea = new javax.swing.JTextArea();
        Background = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("PassHashR | Version One");
        setIconImage(new ImageIcon(getClass().getResource("/passhashr/images/passhashr.png")).getImage()
        );
        setResizable(false);

        try {
            // Set System L&F
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (UnsupportedLookAndFeelException e) {
            // handle exception
        }
        catch (ClassNotFoundException e) {
            // handle exception
        }
        catch (InstantiationException e) {
            // handle exception
        }
        catch (IllegalAccessException e) {
            // handle exception
        }

        Insets noInsets=new Insets(0,0,0,0);
        Encrypt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/passhashr/images/encrypt.png"))); // NOI18N
        Encrypt.setToolTipText("Encrypt Your Characters");
        Encrypt.setBorder(BorderFactory.createEmptyBorder());
        Encrypt.setContentAreaFilled(false);
        Encrypt.setFocusPainted(false);
        Encrypt.setMargin(new java.awt.Insets(0, 0, 0, 0));
        Encrypt.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/passhashr/images/encryptover.png"))); // NOI18N
        Encrypt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EncryptActionPerformed(evt);
            }
        });
        Encrypt.setBounds(457, 12, 170, 35);
        jLayeredPane1.add(Encrypt, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Decrypt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/passhashr/images/decrypt.png"))); // NOI18N
        Decrypt.setToolTipText("Decode Your Characters");
        Decrypt.setBorder(BorderFactory.createEmptyBorder());
        Decrypt.setContentAreaFilled(false);
        Decrypt.setFocusPainted(false);
        Decrypt.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/passhashr/images/decryptover.png"))); // NOI18N
        Decrypt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DecryptActionPerformed(evt);
            }
        });
        Decrypt.setBounds(457, 43, 170, 35);
        jLayeredPane1.add(Decrypt, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Copy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/passhashr/images/copy.png"))); // NOI18N
        Copy.setToolTipText("Copy to Clipboard");
        Copy.setBorder(BorderFactory.createEmptyBorder());
        Copy.setContentAreaFilled(false);
        Copy.setFocusPainted(false);
        Copy.setMargin(new java.awt.Insets(0, 0, 0, 0));
        Copy.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/passhashr/images/copyover.png"))); // NOI18N
        Copy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CopyActionPerformed(evt);
            }
        });
        Copy.setBounds(463, 74, 85, 33);
        jLayeredPane1.add(Copy, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Paste.setIcon(new javax.swing.ImageIcon(getClass().getResource("/passhashr/images/paste.png"))); // NOI18N
        Paste.setToolTipText("Paste Code from Clipboard to Top Box");
        Paste.setBorderPainted(false);
        Paste.setContentAreaFilled(false);
        Paste.setFocusPainted(false);
        Paste.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/passhashr/images/pasteover.png"))); // NOI18N
        Paste.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PasteActionPerformed(evt);
            }
        });
        Paste.setBounds(544, 76, 80, 28);
        jLayeredPane1.add(Paste, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Save.setIcon(new javax.swing.ImageIcon(getClass().getResource("/passhashr/images/save.png"))); // NOI18N
        Save.setToolTipText("Save Encrypted/Decrypted to a File");
        Save.setBorder(BorderFactory.createEmptyBorder());
        Save.setContentAreaFilled(false);
        Save.setFocusPainted(false);
        Save.setMargin(new java.awt.Insets(0, 0, 0, 0));
        Save.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/passhashr/images/Saveover.png"))); // NOI18N
        Save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveActionPerformed(evt);
            }
        });
        Save.setBounds(458, 102, 170, 30);
        jLayeredPane1.add(Save, javax.swing.JLayeredPane.DEFAULT_LAYER);

        SecurityPower.setBackground(new java.awt.Color(0, 0, 0));
        SecurityPower.setForeground(new java.awt.Color(204, 255, 255));
        SecurityPower.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        SecurityPower.setText("5");
        SecurityPower.setToolTipText("Type in a Security Power");
        SecurityPower.setBorder(null);
        SecurityPower.setSelectionColor(new java.awt.Color(46, 82, 94));
        SecurityPower.setBounds(485, 213, 120, 18);
        jLayeredPane1.add(SecurityPower, javax.swing.JLayeredPane.DEFAULT_LAYER);

        OpenSecurityCode.setIcon(new javax.swing.ImageIcon(getClass().getResource("/passhashr/images/opensecutirycode.png"))); // NOI18N
        OpenSecurityCode.setToolTipText("Load a Security Code File");
        OpenSecurityCode.setContentAreaFilled(false);
        OpenSecurityCode.setFocusPainted(false);
        OpenSecurityCode.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/passhashr/images/opensecuritycodeover.png"))); // NOI18N
        OpenSecurityCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OpenSecurityCodeActionPerformed(evt);
            }
        });
        OpenSecurityCode.setBounds(458, 243, 170, 34);
        jLayeredPane1.add(OpenSecurityCode, javax.swing.JLayeredPane.DEFAULT_LAYER);

        GenerateSecurityCode.setIcon(new javax.swing.ImageIcon(getClass().getResource("/passhashr/images/generate.png"))); // NOI18N
        GenerateSecurityCode.setToolTipText("Make a Security Code");
        GenerateSecurityCode.setContentAreaFilled(false);
        GenerateSecurityCode.setFocusPainted(false);
        GenerateSecurityCode.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/passhashr/images/gensecuritycode.png"))); // NOI18N
        GenerateSecurityCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GenerateSecurityCodeActionPerformed(evt);
            }
        });
        GenerateSecurityCode.setBounds(461, 270, 170, 35);
        jLayeredPane1.add(GenerateSecurityCode, javax.swing.JLayeredPane.DEFAULT_LAYER);

        TopScrollPane.setBackground(new java.awt.Color(0, 0, 0));
        TopScrollPane.setBorder(null);
        TopScrollPane.setForeground(new java.awt.Color(204, 255, 255));
        TopScrollPane.setAutoscrolls(true);

        TopTextArea.setBackground(new java.awt.Color(0, 0, 0));
        TopTextArea.setColumns(20);
        TopTextArea.setFont(new java.awt.Font("Verdana", 0, 13));
        TopTextArea.setForeground(new java.awt.Color(204, 255, 255));
        TopTextArea.setLineWrap(true);
        TopTextArea.setRows(5);
        TopTextArea.setSelectionColor(new java.awt.Color(46, 82, 94));
        TopScrollPane.setViewportView(TopTextArea);

        TopScrollPane.setBounds(18, 113, 381, 90);
        jLayeredPane1.add(TopScrollPane, javax.swing.JLayeredPane.DEFAULT_LAYER);

        BottomScrollPane.setBorder(null);

        BottomTextArea.setBackground(new java.awt.Color(0, 0, 0));
        BottomTextArea.setColumns(20);
        BottomTextArea.setFont(new java.awt.Font("Verdana", 0, 13));
        BottomTextArea.setForeground(new java.awt.Color(204, 255, 255));
        BottomTextArea.setLineWrap(true);
        BottomTextArea.setRows(5);
        BottomTextArea.setSelectionColor(new java.awt.Color(46, 82, 94));
        BottomScrollPane.setViewportView(BottomTextArea);

        BottomScrollPane.setBounds(18, 222, 381, 90);
        jLayeredPane1.add(BottomScrollPane, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Background.setIcon(new javax.swing.ImageIcon(getClass().getResource("/passhashr/images/background.png"))); // NOI18N
        Background.setBounds(0, 0, 630, 330);
        jLayeredPane1.add(Background, javax.swing.JLayeredPane.DEFAULT_LAYER);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 624, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 329, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void PasteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PasteActionPerformed
    ClipHelper clip = new ClipHelper();
        TopTextArea.setText(clip.pasteString());
    }//GEN-LAST:event_PasteActionPerformed

    private void GenerateSecurityCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GenerateSecurityCodeActionPerformed
// TODO add your handling code here:
        GenerateSecurityCode();
    }//GEN-LAST:event_GenerateSecurityCodeActionPerformed
    private void GenerateSecurityCode(){
        String s = SecurityPower.getText();
        int t = Integer.parseInt(s);
        if(t <= 1)
            t =2;
        if(t <= 2000000)
            t = 2000000;
        letters = rg.generatecodes(Integer.parseInt(s));
        JFileChooser chooser = new JFileChooser();
        int returnVal = chooser.showSaveDialog(PassHashRGUI.this);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            System.out.println("You chose to save this file: " +
            chooser.getSelectedFile().getName());
            file = chooser.getSelectedFile();
            io.DumptoFile(letters, file);
        }
        ranonce = true;
    }
    private void OpenSecurityCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OpenSecurityCodeActionPerformed
        OpenSecurityCode();
    }//GEN-LAST:event_OpenSecurityCodeActionPerformed
    private void OpenSecurityCode(){
        JFileChooser chooser = new JFileChooser();
        int returnVal = chooser.showOpenDialog(PassHashRGUI.this);
            if(returnVal == JFileChooser.APPROVE_OPTION) {
                System.out.println("You chose to read this file: " +
                chooser.getSelectedFile().getName());
                file = chooser.getSelectedFile();
                letters = io.ReadFile(file);
                ranonce = true;
            }
    }
   
    private void SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveActionPerformed
        
        
        JFileChooser chooser = new JFileChooser();
            int returnVal = chooser.showSaveDialog(PassHashRGUI.this);
            if(returnVal == JFileChooser.APPROVE_OPTION) {
                System.out.println("You chose to save this file: " +
                chooser.getSelectedFile().getName());
                File f = chooser.getSelectedFile();
                io.write2file(BottomTextArea.getText(), f);
            }
    }//GEN-LAST:event_SaveActionPerformed

    private void CopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CopyActionPerformed
// TODO add your handling code here:
        ClipHelper clip = new ClipHelper();
        clip.copyString(BottomTextArea.getText());
    }//GEN-LAST:event_CopyActionPerformed

    private void DecryptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DecryptActionPerformed
       if(!ranonce)
           OpenSecurityCode();
       BottomTextArea.setText(pshr.decode(TopTextArea.getText(), file));
       ranonce = true;
    }//GEN-LAST:event_DecryptActionPerformed

    private void EncryptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EncryptActionPerformed
// TODO add your handling code here:
        if(!ranonce){
            String yesno = JOptionPane.showInputDialog("Do you want to load or generate a Security Code?");
            if(yesno.startsWith("l") || yesno.startsWith("L"))
                OpenSecurityCode();
            else     
                GenerateSecurityCode();
        }
        BottomTextArea.setText(pshr.encode(TopTextArea.getText(), letters));
        ranonce = true;
    }//GEN-LAST:event_EncryptActionPerformed
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PassHashRGUI().setVisible(true);
            }
        });
    }
    
    

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Background;
    private javax.swing.JScrollPane BottomScrollPane;
    private javax.swing.JTextArea BottomTextArea;
    private javax.swing.JButton Copy;
    private javax.swing.JButton Decrypt;
    private javax.swing.JButton Encrypt;
    private javax.swing.JButton GenerateSecurityCode;
    private javax.swing.JButton OpenSecurityCode;
    private javax.swing.JButton Paste;
    private javax.swing.JButton Save;
    private javax.swing.JTextField SecurityPower;
    private javax.swing.JScrollPane TopScrollPane;
    private javax.swing.JTextArea TopTextArea;
    private javax.swing.JLayeredPane jLayeredPane1;
    // End of variables declaration//GEN-END:variables

}
